package com.wika.basics.widget

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.drawable.Drawable
import android.graphics.drawable.GradientDrawable
import android.graphics.drawable.StateListDrawable
import android.text.TextUtils
import android.util.AttributeSet
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.RelativeLayout
import android.widget.TextView
import androidx.core.content.ContextCompat
import com.wika.basics.R

/**
 * @Description: 自定义图片文字按钮
 * @Date: 2023/10/14 14:28
 * @Author: WangWeiShuo
 */
@SuppressLint("SuspiciousIndentation")
class WikaTopImageBottomTextButton @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : LinearLayout(context, attrs, defStyleAttr) {
    private var radius = 0
    private var textSize = 10
    private var bgColor = 0
    private var bgClickColor = 0
    private var textColor = 0
    private var textMsg: String? = null
    private var drawable: Drawable? = null
    private var rlImage: RelativeLayout? = null
    private var ivIcon: ImageView? = null
    private var textView: TextView? = null
    private var stateListDrawable = StateListDrawable()

    init {
        initAttrs(context, attrs)
        val view = LayoutInflater.from(context).inflate(R.layout.widget_top_image_bottom_text, this)
        initView(context, view)
    }

    private fun initAttrs(context: Context, attrs: AttributeSet?) {
        val typedArray = context.obtainStyledAttributes(attrs, R.styleable.TopImageBottomTextButton)
        if(typedArray != null) {
            radius = typedArray.getDimensionPixelSize(
                R.styleable.TopImageBottomTextButton_cornerRadius,
                0
            )
            textSize =
                typedArray.getDimensionPixelSize(R.styleable.TopImageBottomTextButton_textSize, 20)
            bgColor = typedArray.getColor(R.styleable.TopImageBottomTextButton_normalColor, -1)
            bgClickColor = typedArray.getColor(
                R.styleable.TopImageBottomTextButton_pressedColor,
                ContextCompat.getColor(context, R.color.gray_27000000)
            )
            textColor = typedArray.getColor(
                R.styleable.TopImageBottomTextButton_textColor,
                ContextCompat.getColor(context, R.color.black)
            )
            textMsg = typedArray.getString(R.styleable.TopImageBottomTextButton_text)
            drawable = typedArray.getDrawable(R.styleable.TopImageBottomTextButton_center_icon)
            val borderColor = typedArray.getColor(
                R.styleable.TopImageBottomTextButton_borderColor,
                ContextCompat.getColor(context, R.color.white)
            )
            val borderWidth = typedArray.getDimensionPixelSize(
                R.styleable.TopImageBottomTextButton_borderWidth,
                0
            )
            //生成圆角图片
            val normalDrawable = GradientDrawable()
            normalDrawable.setColor(bgColor)
            normalDrawable.setStroke(borderWidth, borderColor)
            normalDrawable.cornerRadius = radius.toFloat()

            //生成一张半透明的颜色 可自定义
            val pressedDrawable = GradientDrawable()
            pressedDrawable.setColor(bgClickColor)
            pressedDrawable.cornerRadius = radius.toFloat()


            //设置点击后的效果
//        val stateListDrawable = StateListDrawable()
            stateListDrawable.addState(intArrayOf(android.R.attr.state_pressed), pressedDrawable)
            stateListDrawable.addState(intArrayOf(), normalDrawable)
//        background = stateListDrawable
            typedArray.recycle()
        }
    }

    override fun onLayout(changed: Boolean, l: Int, t: Int, r: Int, b: Int) {
        super.onLayout(changed, l, t, r, b)
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        if (rlImage != null) {
            rlImage!!.layoutParams = LayoutParams(
                measuredWidth, measuredWidth
            )
        }
    }

//    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    private fun initView(context: Context, view: View) {
        ivIcon = view.findViewById(R.id.iv_icon)
        textView = view.findViewById(R.id.image_text)
        rlImage = view.findViewById(R.id.rl_image)
        rlImage?.background = stateListDrawable
//        rlImage?.clipToOutline = true

//        rlImage?.outlineProvider = object : ViewOutlineProvider() {
//            override fun getOutline(view: View, outline: Outline) {
//                outline.setRoundRect(0, 0, view.width, view.height, radius.toFloat())
//            }
//        }

//    if (Build.VERSION.SDK_INT >= 28) {
//        // Api 28 以上设置阴影颜色
//        view.outlineSpotShadowColor = bgClickColor
//    }

//        if (bgColor != -1) {
//            rlImage?.setBackgroundColor(bgColor)
//        }
        if (drawable != null) {
            ivIcon?.background = drawable
        }

        if (!TextUtils.isEmpty(textMsg)) {
            setText(textMsg)
        }
    }

    fun setText(txt: String?) {
        if (textView != null) {
            textView!!.text = txt
            textView!!.setTextColor(textColor)
            textView!!.textSize = textSize.toFloat()
        }
    }
}